package Server.Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Used to query MySQL database.
 *
 * @author PZK
 *
 */
public class Database {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    public SettingsRegister settingsRegister;
    public ProjectRegister projectRegister;
    public UsersRegister usersRegister;
    public PhraseRegister phraseRegister;
    public MessagesRegister messagesRegister;
    public Database(String url, String username, String password) throws SQLException, ClassNotFoundException {

        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(url, username, password);
        statement = connection.createStatement();
        this.phraseRegister = new PhraseRegister(this);
        this.projectRegister = new ProjectRegister(this);
        this.usersRegister = new UsersRegister(this);
        this.settingsRegister = new SettingsRegister(this);
        this.messagesRegister = new MessagesRegister(this);

    }

    public void closeResults() throws SQLException {

        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }

    }
}
